<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Activity_edit extends CI_Controller
{
	private $user_id;
	function __construct()
	{
		parent::__construct();
		
		$this->load->config('layout', TRUE);
		$this->load->helper('url');
		$this->load->library('security');
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
		$this->load->model('activity_model');
		$this->user_id = $this->session->userdata('user_id');
	}

	function index($action='new',$activity_id='')
	{
		if (!$this->tank_auth->is_logged_in()) {									// not logged in
			redirect('');
		} 
		else{
			$data = $this->session->all_userdata();
			$this->load->view('coplay/header', $data);
			
			
			if($action != 'new' && !empty($activity_id)){
				$data['activity'] = $this->get_activity_by_id($activity_id);
				
				if(empty($data['activity'])){
						redirect('/activity_edit');
				}			
	
				$data['upload_image'] = json_decode($data['activity'][0]['upload_image']);
				$data['image_uploader'] = $this->load->view('coplay/activity_edit/image_uploader', $data,true);
				$data['activity_id'] = $activity_id;
				//using different activity edit view
				$data['activity_design'] = $this->load->view('coplay/activity_edit/activity_modify', $data ,true);
			}
			else{// establish a new activity
				$data['image_uploader'] = $this->load->view('coplay/activity_edit/image_uploader', $data,true);
				$data['activity_design'] = $this->load->view('coplay/activity_edit/activity_design', $data ,true);
			}
			
			$data['action_msg'] = $this->session->flashdata('action_msg');
			$this->load->view('coplay/activity_edit/index', $data);
		}
	}
	
	function cancel_join(){
		$post_data = $this->input->post(NULL,true);
		
		if($this->activity_model->delete_by_kind($post_data['activity_id'],$post_data['kind'])){ //delete successfully
			$activity['user_id'] = $this->user_id;
			$activity['activities'][$post_data['kind']] = $this->activity_model->get_by_kind($post_data['kind'],$this->user_id);
			echo $this->load->view('coplay/personal/activity_'.$post_data['kind'], $activity,true);
		}
		else{ // delete failed
			$activity['user_id'] = $this->user_id;
			$activity['activities'][$post_data['kind']] = $this->activity_model->get_by_kind($post_data['kind'],$this->user_id);
			$error_msg = '<div class="error_msg"><p>退出失敗，請稍候再試或洽管理人員。</p></div>';
			echo $error_msg.$this->load->view('coplay/personal/activity_'.$post_data['kind'], $activity,true);
		}
	}
	
	function delete(){
		$post_data = $this->input->post(NULL,true);
		
		if($this->activity_model->delete_by_kind($post_data['activity_id'],$post_data['kind'])){ //delete successfully
			$activity['user_id'] = $this->user_id;
			$activity['activities'][$post_data['kind']] = $this->activity_model->get_by_kind($post_data['kind'],$this->user_id);
			echo $this->load->view('coplay/personal/activity_'.$post_data['kind'], $activity,true);
		}
		else{ // delete failed
			$activity['user_id'] = $this->user_id;
			$activity['activities'][$post_data['kind']] = $this->activity_model->get_by_kind($post_data['kind'],$this->user_id);
			$error_msg = '<div class="error_msg"><p>刪除失敗，請稍候再試或洽管理人員。</p></div>';
			echo $error_msg.$this->load->view('coplay/personal/activity_'.$post_data['kind'], $activity,true);
		}
	}
	
	function modify($activity_id='')
	{
		$user_id = $this->user_id;
		$this->form_validation->set_rules('activity_title', '標題', 'trim|required|xss_clean|strip_tags');
		$this->form_validation->set_rules('activity_date', '日期', 'trim|required|xss_clean|valid_date|strip_tags');
		$this->form_validation->set_rules('activity_time', '日期時間', 'trim|required|xss_clean|strip_tags');
		$this->form_validation->set_rules('activity_duration', '時間', 'trim|required|xss_clean|integer|strip_tags');
		$this->form_validation->set_rules('activity_location', '地點', 'trim|required|xss_clean|strip_tags');
		$this->form_validation->set_rules('activity_category', '分類', 'trim|required|xss_clean|strip_tags');
		$this->form_validation->set_rules('activity_min_p', '最少人數', 'trim|required|xss_clean|integer|strip_tags');
		$this->form_validation->set_rules('activity_max_p', '最多人數', 'trim|required|xss_clean|integer|strip_tags');
		$this->form_validation->set_rules('activity_price', '價錢', 'trim|required|xss_clean|integer|strip_tags');
		$this->form_validation->set_rules('desc', '活動敘述', 'trim|required');
		
		$activity_data = $this->input->post();
		if ($this->form_validation->run()) { // validate ok
			$this->form_validation->_field_data = array(); // unset validated data
		
			$activity_data['user_id'] = $user_id;
			$this->activity_model->modify($activity_data,$activity_id);
			if(isset($activity_data['upload_image'])){
				$this->move_images($activity_data['upload_image']);	
			}
			
			$this->session->set_flashdata('action_msg', '修改成功');
			
			redirect('/personal');
		}
		else{
			$data = array();	
			$data['user_id'] = $user_id;
			$data['upload_image'] = isset($activity_data['upload_image'])?$activity_data['upload_image']:'';
			$this->load->view('coplay/header', '');
			$data['image_uploader'] = $this->load->view('coplay/activity_edit/image_uploader', $data,true);
			$data['activity_design'] = $this->load->view('coplay/activity_edit/activity_design', $data ,true);
			//$data['activities'] = $this->get_activity($data['user_id']);
			//$data['activity_manage'] = $this->load->view('coplay/activity_edit/activity_manage', $data ,true);
			
			$this->load->view('coplay/activity_edit/index/modify/'.$activity_id, $data);	
		}
	}
	
	function save(){
		//echo "<pre>";print_r($this->input->post());echo "</pre>";exit;
		$user_id = $this->user_id;
		
		$this->form_validation->set_rules('activity_title', '標題', 'trim|required|xss_clean|strip_tags');
		$this->form_validation->set_rules('activity_date', '日期', 'trim|required|xss_clean|valid_date|strip_tags');
		$this->form_validation->set_rules('activity_time', '日期時間', 'trim|required|xss_clean|strip_tags');
		$this->form_validation->set_rules('activity_duration', '時間', 'trim|required|xss_clean|integer|strip_tags');
		$this->form_validation->set_rules('activity_location', '地點', 'trim|required|xss_clean|strip_tags');
		$this->form_validation->set_rules('activity_category', '分類', 'trim|required|xss_clean|strip_tags');
		$this->form_validation->set_rules('activity_min_p', '最少人數', 'trim|required|xss_clean|integer|strip_tags');
		$this->form_validation->set_rules('activity_max_p', '最多人數', 'trim|required|xss_clean|integer|strip_tags');
		$this->form_validation->set_rules('activity_price', '價錢', 'trim|required|xss_clean|integer|strip_tags');
		$this->form_validation->set_rules('desc', '活動敘述', 'trim|required');
		
		$activity_data = $this->input->post();
		if ($this->form_validation->run()) { // validate ok
			$this->form_validation->_field_data = array(); // unset validated data
		
			$activity_data['user_id'] = $user_id;
			//echo "<pre>";print_r($activity_data);echo "</pre>";exit;
			$this->activity_model->save($activity_data);
			if(isset($activity_data['upload_image'])){
				$this->move_images($activity_data['upload_image']);	
			}
			
			$this->session->set_flashdata('action_msg', '新增成功');
			redirect('/personal');
		}
		else{
			$data = array();	
			$data['user_id'] = $user_id;
			$data['upload_image'] = isset($activity_data['upload_image'])?$activity_data['upload_image']:'';
			$this->load->view('coplay/header', '');
			$data['image_uploader'] = $this->load->view('coplay/activity_edit/image_uploader', $data,true);
			$data['activity_design'] = $this->load->view('coplay/activity_edit/activity_design', $data ,true);
			//$data['activities'] = $this->get_activity($data['user_id']);
			//$data['activity_manage'] = $this->load->view('coplay/activity_edit/activity_manage', $data ,true);
			
			$this->load->view('coplay/activity_edit/index', $data);	
		}
	}

	function save_as_draft(){
		//echo "<pre>";print_r($this->input->post());echo "</pre>";exit;
		if (!$this->tank_auth->is_logged_in()) {									// not logged in
			redirect('');
		} 
		else{
			$user_id = $this->user_id;
			$activity_data = $this->input->post();
			$this->form_validation->_field_data = array(); // unset validated data
			
			$activity_data['user_id'] = $user_id;
			$activity_data['status'] = '0'; // status assign 0 as draft
			//echo "<pre>";print_r($activity_data);echo "</pre>";exit;
			$this->activity_model->save($activity_data);
			if(isset($activity_data['upload_image'])){
				$this->move_images($activity_data['upload_image']);	
			}
			
			$this->session->set_flashdata('action_msg', '已存為草稿');
			redirect('/personal');
		}
	}
	
	function move_images($filenames) // move images from temp to original and thumbs directory
	{
		$user_id = $this->user_id;
		$tempDirectory = $_SERVER["DOCUMENT_ROOT"].$this->config->item('image_uploader_temp', 'layout').$user_id.'/';
		$originalImageDirectory = $_SERVER["DOCUMENT_ROOT"].$this->config->item('image_uploader_original', 'layout').$user_id.'/';
		$thumbsImageDirectory = $_SERVER["DOCUMENT_ROOT"].$this->config->item('image_uploader_thumbs', 'layout').$user_id.'/';
		if (!is_writable($originalImageDirectory)){
			mkdir($originalImageDirectory);
        }
		
		if (!is_writable($thumbsImageDirectory)){
			mkdir($thumbsImageDirectory);
        }
		
		foreach($filenames as $filename){
			@rename($tempDirectory.$filename, $originalImageDirectory.$filename);// move to original
			@rename($tempDirectory.'thumbs_'.$filename, $thumbsImageDirectory.$filename);// move to thumbs
		}
		
		return;
	}
	
	function get_activity(){
		$activities = $this->activity_model->get($this->user_id);
		return $activities;
	}
	
	function get_activity_by_id($activity_id){
		$activities = $this->activity_model->get_by_id($activity_id,$this->user_id);
		return $activities;
	}
}